import request from '@/utils/request'

export const getAndengData = () => {
  return request({
    url: 'PRDMonitor/SITUATION/list',
    method: 'get',
    timeout: 20000
  })
}

export const getParmas = () => {
  return request({
    url: 'interface/api/planTargetList',
    method: 'get'
  })
}

const enumTimeMap = new Map([
  ['1区', "one"],
  ['2区', "two"],
  ['3区', "three"],
  ['4区', "four"],
  ['5区', "five"],
  ['6区', "six"]
])

export const getMbbPosition = (data: any[]) => {
  let firstLine: any[] = []
  let secondLine: any[] = []
  let thirdLine: any[] = []
  let fourthLine: any[] = []

  let excludeFirstLine = ['H05-MBB01', 'H05-MBB03', 'H05-MBB05', 'H05-MBB07', 'H05-MBB09']
  let excludeSecondLine = ['H05-MBB02', 'H05-MBB04', 'H05-MBB06', 'H05-MBB08', 'H05-MBB10']
  let excludeThirdLine = ['H05-MBB11', 'H05-MBB13', 'H05-MBB15', 'H05-MBB17', 'H05-MBB19', 'H05-MBB21']
  let excludeFourthLine = ['H05-MBB12', 'H05-MBB14', 'H05-MBB16', 'H05-MBB18', 'H05-MBB20', 'H05-MBB22']
  data.forEach((item:any) => {
    if(excludeFirstLine.includes(item.eqpId)){
      firstLine.push(item)
    } else if(excludeSecondLine.includes(item.eqpId)){
      secondLine.push(item)
    } else if(excludeThirdLine.includes(item.eqpId)){
      thirdLine.push(item)
    } else if(excludeFourthLine.includes(item.eqpId)){
      fourthLine.push(item)
    }
  })

  return [firstLine, secondLine, thirdLine, fourthLine]
}

export const getDieWaPosition = (data: any[]) => {
  let firstLine: any[] = []
  let secondLine: any[] = []
  let thirdLine: any[] = []
  let fourthLine: any[] = []

  let excludeFirstLine = ['H05-DWHJ01', 'H05-DWHJ05', 'H05-DWHJ09', 'H05-DWHJ13', 'H05-DWHJ17']
  let excludeSecondLine = ['H05-DWHJ02', 'H05-DWHJ06', 'H05-DWHJ10', 'H05-DWHJ14', 'H05-DWHJ18']
  let excludeThirdLine = ['H05-DWHJ03', 'H05-DWHJ07', 'H05-DWHJ11', 'H05-DWHJ15', 'H05-DWHJ19']
  let excludeFourthLine = ['H05-DWHJ04', 'H05-DWHJ08', 'H05-DWHJ12', 'H05-DWHJ16', 'H05-DWHJ20']
  data.forEach((item:any) => {
    if(excludeFirstLine.includes(item.eqpId)){
      firstLine.push(item)
    } else if(excludeSecondLine.includes(item.eqpId)){
      secondLine.push(item)
    } else if(excludeThirdLine.includes(item.eqpId)){
      thirdLine.push(item)
    } else if(excludeFourthLine.includes(item.eqpId)){
      fourthLine.push(item)
    }
  })

  return [firstLine, secondLine, thirdLine, fourthLine]
}

export const getTables = (data: any, name:string) => {
  let target:any = [{ classes: '进度目标' }, { classes: '进度差异' }, { classes: '进度达成率' }]
  data.forEach((value:any) => {
    if(value.eqpType === name){
      value.oeeMonitorPercentageList.forEach((item:any, idx:number) => {
        target[0][`${idx + 1}areaEarly`] = item.dyTargetProcess
        target[0][`${idx + 1}areaNight`] = item.gyTargetProcess
        target[0][`${idx + 1}areaAllDay`] = item.ttlTargetProcess

        target[1][`${idx + 1}areaEarly`] = item.dyDiff
        target[1][`${idx + 1}areaNight`] = item.gyDiff
        target[1][`${idx + 1}areaAllDay`] = item.ttlDiff

        target[2][`${idx + 1}areaEarly`] = item.dyDiffPercentage
        target[2][`${idx + 1}areaNight`] = item.gyDiffPercentage
        target[2][`${idx + 1}areaAllDay`] = item.ttlDiffPercentage
      })

    }
  })
  return target
}

const cellStyle = ({ colDef, value }: any) => {
  if(colDef.field === 'warningSum' || colDef.field === 'earlyWarningSum'){
    if(value > 0){
      return {
        color: 'red',
        fontWeight: 700
      }
    }
  }
  if(colDef.field === 'runSumPercentage'){
    let target = value.split('%').join('')
    if(Number(target) < 85){
      return {
        background: '#ecb665'
      }
    }
  }
}

export const column = [
  { headerName: "制造区域", field: "process", minWidth: 70, flex: 1 },
  { headerName: "设备数", field: "eqpSum", minWidth: 60, flex: 1 },

  {
    headerName: "生产进度(万片)", headerClass: 'custom-moving',
    children: [
      { headerName: "全天目标", field: "targetMoving", minWidth: 50, flex: 1, headerClass: 'custom-moving' },
      { headerName: "实际产量", field: "productMoving", minWidth: 50, flex: 1, headerClass: 'custom-moving' },
      { headerName: "产量差异", field: "diffMoving", minWidth: 50, flex: 1, headerClass: 'custom-moving' },
      { headerName: "进度目标", field: "processMoving", minWidth: 50, flex: 1, headerClass: 'custom-moving' },
      { headerName: "进度达成率", field: "finishPercentage", minWidth: 50, flex: 1, headerClass: 'custom-moving' },
      { headerName: "进度差异", field: "processDiff", minWidth: 50, flex: 1, headerClass: 'custom-moving' }
    ]
  },

  {
    headerName: "预警现况",
    children: [
      { headerName: "告警", field: "warningSum", minWidth: 50, flex: 1, cellStyle },
      { headerName: "预警", field: "earlyWarningSum", minWidth: 50, flex: 1, cellStyle },
      { headerName: "正常", field: "normalSum", minWidth: 50, flex: 1 },
      { headerName: "超产", field: "overProductionSum", minWidth: 50, flex: 1 }
    ]
  },
  {
    headerName: "RUN",
    children: [
      { headerName: "台数", field: "runSum", minWidth: 50, flex: 1 },
      { headerName: "比例", field: "runSumPercentage", minWidth: 50, flex: 1, cellStyle }
    ]
  },
  {
    headerName: "DOWN",
    children: [
      { headerName: "台数", field: "dowmSum", minWidth: 50, flex: 1 },
      { headerName: "比例", field: "dowmSumPercentage", minWidth: 50, flex: 1 }
    ]
  },

  {
    headerName: "PM",
    children: [
      { headerName: "台数", field: "pmSum", minWidth: 50, flex: 1 },
      { headerName: "比例", field: "pmSumPercentage", minWidth: 50, flex: 1 }
    ]
  },

  {
    headerName: "IDLE",
    children: [
      { headerName: "台数", field: "idleSum", minWidth: 50, flex: 1 },
      { headerName: "比例", field: "idleSumPercentage", minWidth: 50, flex: 1 }
    ]
  },

  {
    headerName: "OFF",
    children: [
      { headerName: "台数", field: "offSum", minWidth: 50, flex: 1 },
      { headerName: "比例", field: "offSumPercentage", minWidth: 50, flex: 1 }
    ]
  }
]

const cellStyleAllDay = ({ data }: any) => {
  if(data.classes === '进度差异'){
    return {
      fontWeight: 500,
      color: 'blue',
      textDecoration: 'underline',
      cursor: 'pointer',
      lineHeight: '32px'
    }
  } else {
    return {
      lineHeight: '32px'
    }
  }
}

const commonColumn = [
  {
    headerName: "1区",
    children: [
      { headerName: "早班", field: "1areaEarly", minWidth: 50, flex: 1 },
      { headerName: "夜班", field: "1areaNight", minWidth: 50, flex: 1 },
      { headerName: "全天", field: "1areaAllDay", minWidth: 50, flex: 1, cellStyle: cellStyleAllDay }
    ]
  },
  {
    headerName: "2区",
    children: [
      { headerName: "早班", field: "2areaEarly", minWidth: 50, flex: 1 },
      { headerName: "夜班", field: "2areaNight", minWidth: 50, flex: 1 },
      { headerName: "全天", field: "2areaAllDay", minWidth: 50, flex: 1, cellStyle: cellStyleAllDay }
    ]
  },
  {
    headerName: "3区",
    children: [
      { headerName: "早班", field: "3areaEarly", minWidth: 50, flex: 1 },
      { headerName: "夜班", field: "3areaNight", minWidth: 50, flex: 1 },
      { headerName: "全天", field: "3areaAllDay", minWidth: 50, flex: 1, cellStyle: cellStyleAllDay }
    ]
  },
  {
    headerName: "4区",
    children: [
      { headerName: "早班", field: "4areaEarly", minWidth: 50, flex: 1 },
      { headerName: "夜班", field: "4areaNight", minWidth: 50, flex: 1 },
      { headerName: "全天", field: "4areaAllDay", minWidth: 50, flex: 1, cellStyle: cellStyleAllDay }
    ]
  },
  {
    headerName: "5区",
    children: [
      { headerName: "早班", field: "5areaEarly", minWidth: 50, flex: 1 },
      { headerName: "夜班", field: "5areaNight", minWidth: 50, flex: 1 },
      { headerName: "全天", field: "5areaAllDay", minWidth: 50, flex: 1, cellStyle: cellStyleAllDay }
    ]
  }
]

export const mbbColumn = [
  {
    headerName: "MBB", headerClass: 'custom-title',
    children: [
      { headerName: "班次", field: "classes", minWidth: 50, flex: 1 }
    ]
  },
  ...commonColumn,
  {
    headerName: "6区",
    children: [
      { headerName: "早班", field: "6areaEarly", minWidth: 50, flex: 1 },
      { headerName: "夜班", field: "6areaNight", minWidth: 50, flex: 1 },
      { headerName: "全天", field: "6areaAllDay", minWidth: 50, flex: 1, cellStyle: cellStyleAllDay }
    ]
  }
]

export const dewaColumn = [
  {
    headerName: "叠瓦焊机", headerClass: 'custom-title',
    children: [
      { headerName: "班次", field: "classes", minWidth: 50, flex: 1 }
    ]
  },
  ...commonColumn
]